//
//  WmcPhotoSelectManager.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/4/7.
//

import Foundation
import TZImagePickerController
import YPBedrock

// MARK: - [WmcPhotoSelectManager]

/// 照片选择器管理者
open class WmcPhotoSelectManager: NSObject {
    /// 选中图片回调Block
    public typealias PhotoSelectBlock = ([UIImage]) -> Void

    // Open

    open func photoSelect(vc: UIViewController? = nil, maxCount: Int, selectBlock: PhotoSelectBlock? = nil, allowTakePicture: Bool = true) {
        let presentVC = vc ?? YPApp.CurrentVC
        photoSelectBlock = selectBlock
        if let imgSelectVC = TZImagePickerController(maxImagesCount: maxCount, columnNumber: 4, delegate: self, pushPhotoPickerVc: true) {
            imgSelectVC.allowPickingOriginalPhoto = false
            imgSelectVC.allowPickingVideo = false
            imgSelectVC.allowPickingGif = false
            imgSelectVC.allowTakeVideo = false
            imgSelectVC.showSelectBtn = true
            imgSelectVC.showPhotoCannotSelectLayer = true
            imgSelectVC.allowTakePicture = allowTakePicture
            imgSelectVC.modalPresentationStyle = .fullScreen
            presentVC?.present(imgSelectVC, animated: true, completion: nil)
        }
    }

    // Public

    public static let shared: WmcPhotoSelectManager = .init()

    public static func photoSelect(vc: UIViewController? = nil, maxCount: Int, selectBlock: PhotoSelectBlock? = nil, allowTakePicture: Bool = true) {
        shared.photoSelect(vc: vc, maxCount: maxCount, selectBlock: selectBlock, allowTakePicture: allowTakePicture)
    }

    // Internal

    // 选中图片回调Block
    var photoSelectBlock: PhotoSelectBlock?

    /// 取消回调Block
    var cancelBlock: (() -> Void)?
}

// MARK: - TZImagePickerControllerDelegate

extension WmcPhotoSelectManager: TZImagePickerControllerDelegate {
    public func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool) {
        photoSelectBlock?(photos)
    }

    public func tz_imagePickerControllerDidCancel(_ picker: TZImagePickerController!) {
        cancelBlock?()
    }
}
